<template>
	<view class="allpage">
		<view class="center">
				<view class="logo" align="center">
					<view style="width: 348rpx;height: 100rpx;margin: 32rpx auto 38rpx;">
						<image src="../../static/images/jx_logo.png" mode="" style="width: 348rpx;height: 100rpx;"></image>
					</view>
					<view class="logo-name">申请获取您的公开信息（头像，昵称等)</view>
				</view>
				<view class="btn-box">
					<!-- v-if="authorizeType == 0" -->
				<button class="btn-ok btn-ok-action" open-type="getUserProfile" sty hover-class="none"
				 @tap="appLogin" v-if="authorizeType == 0" :disabled="loginDisabled">微信授权</button>
				<button class="btn-ok btn-ok-action" open-type="getPhoneNumber" :disabled="PhoneDisabled"
				hover-class="none" @getphonenumber="getPhoneNumber" v-if="authorizeType == 1">授权手机号</button>
			</view>	
		</view>
		<view class="info-box">
			<view style="font-size: 30rpx;color: #323332;margin-bottom: 10rpx;font-family: PingFang SC;">服务说明</view>
			<view style="font-size: 26rpx;color: #323332;font-family: PingFang SC;">
				<view>1. 此小程序仅作为企业内部员工挑选工装使用；</view>
				<view>2. 需要微信授权登录并认证为企业员工即可进入系统并使用。</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				showPhone:false,
				isCode:'',
				authorizeType:0,//微信一键登录0,获取手机号码1
				session_key:'',
				loginDisabled:false,
				PhoneDisabled:false,
			}
		},
		onLoad() {
			
		},
		methods:{
			getLogoImage(){
			},
			getToken(){
				var self = this;
				self.loginDisabled=true;
				uni.request({
					url: self.siteBaseUrl + '/wxApp/code2Session',
					method: 'GET',
					data: {
						code :self.isCode,
					},
					success: res => {
						console.log(res.data.status,'res=>2323232131')
						if (res.data.status == "SUCCEED") {//用户存在
							uni.setStorageSync('access_token',res.data.result.token);
							self.session_key=res.data.result.session_key;
							self.authorizeType=self.authorizeType+1;
							console.log(self.authorizeType,'self.authorizeType>>>>>>>')
							self.loginDisabled=false;
							// uni.setStorageSync('bound_phone',res.data.bound_phone);
						}
					},
					fail:function(e){
						console.log("微信login接口调用失败:" + JSON.stringify(e));
					},
				})
			},
			appLogin(){
				var self = this;
				wx.login({
				    success:function(res){
						console.log(res,'微信登录获取code')
				        self.isCode = res.code;
						wx.showModal({
							title: '温馨提示',
							content: '你好，授权微信登录后才能正常使用小程序功能',
							success(res) {
								console.log(res)
								//如果用户点击了确定按钮
								if (res.confirm) {
									wx.getUserProfile({
										desc: '获取你的昵称、头像、地区及性别',
										success: infoRes => {
											console.log(infoRes);
											self.getToken();
											var stUSER = infoRes.userInfo;
											uni.setStorageSync('userInfo',stUSER);
										},
										fail: infoRes => {
											console.log(infoRes)
											//拒绝授权
											self.getToken();
											self.show = false;
											self.showPhone = true;
											uni.showModal({
												title: '提示',
												content: '您拒绝了授权用户信息，一些功能将无法使用',
												success: function (res) {
													uni.openSetting({
													  success(res) {
														console.log(res.authSetting)
													  }
													});
												}
											});
										}
									});
								}
							}
						});
					} 
				})
			},
			getPhoneNumber(e){
				var that = this;
				that.PhoneDisabled=true;
				console.log(e)
				var userinfopre = uni.getStorageSync('userInfo');
				console.log(userinfopre)
				console.log(typeof userinfopre)
				if(e.detail.errMsg=="getPhoneNumber:fail user deny"){//用户拒绝授权
					uni.showModal({
					    title: '温馨提示',
					    content: '您点击了拒绝授权，将无法正常使用小程序，点击重新获取授权',
					    success: function (res) {
							uni.openSetting({
							  success(res) {
							    
							  }
							});
					    }
					});
				}else{
					that.sendRequest({
				        url : "/wxApp/userInfo",
				        method : "GET",
				        data : {
							ivData  : e.detail.iv,
							encryptData:e.detail.encryptedData,
							sessionKey :that.session_key,
						},
				        hideLoading : true,
				        success:function (res) {
							console.log(res.result)
							that.showPhone = false;
							that.PhoneDisabled = false;
							// uni.setStorageSync('access_token',res.access_token);
							uni.setStorageSync('phoneNumber',res.result.phoneNumber);
							uni.setStorageSync('watermark',res.result.watermark)
							uni.setStorageSync('phineLogin',true);//判断是否授权手机号
							uni.setStorageSync('wxLogin',true);//判断是否第一次进入授权
							that.getWxInfo(res.result.phoneNumber)
							
							
				            
				        }
				})	
				}
			},
			getWxInfo(phoneNumber){
				var that = this;
				const userInfo=uni.getStorageSync('userInfo');
				console.log(userInfo);
				that.sendRequest({
					url : "/member/saveWxInfo",
					method : "POST",
					data : {
						avatar:userInfo.avatarUrl,
						nickname:userInfo.nickName,
						phone: phoneNumber
					},
					hideLoading : true,
					success:function (res) {
						console.log(res,'======>>>>>>')
						if(res.result=='保存成功'){
							that.getMemberCheck();
						}
					},
					fail:function(res){
						
					}
				})
				
			},
			getMemberCheck(){
				var that = this;
				that.sendRequest({
					url : "/member/check",
					method : "POST",
					data : {
					},
					hideLoading : true,
					success:function (res) {
						console.log(res.result)
						var checkResult=res.result;
						uni.setStorageSync('checkResult',checkResult);
						if(res.result==null || res.result=={} ){
							uni.redirectTo({
								url:'../Authorization/Authorization'
							})	
						}else{
							uni.switchTab({
								url:'../index/index'
							})
						}
					}
				})
				
			}
			
		}
	}
</script>

<style>
	.allpage{
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		/* background-image:url(../../static/images/bg.png); */
		background-repeat: no-repeat;
		background:url(http://124.71.202.247:8080/image/6c93008235d2484a812ebd588de796f2.png) no-repeat left top;
		background-size: 100% 100%;
		padding: 0 30rpx;
	}
	.center {
		padding: 32rpx 30rpx 74rpx;
	    margin-top: 60%;
	    background: #ffffff;
	    border-radius: 24rpx;
	}
	.info-box{
		margin-top: 80rpx;
		padding: 32rpx;
		border-radius: 24rpx;
		background-color: #FFFFFF;
	}
	.logo{
		display: flex;
		flex-direction: column;
		justify-content: center;
	}
	.logo-name{
		width: 100%;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 50rpx;
		color: #323332;
		text-align: center;
	}
	
	.btn-box {
		margin-top: 50rpx;
	}
	.btn-ok-action{
		width:100%;
		height: 80rpx;
		text-align: center;
		color: #FFFFFF;
		font-size: 32rpx;
		background: linear-gradient(89deg, #4EBFF9 0%, #4590E8 100%);
		box-shadow: 0px 6px 12px rgba(14, 92, 183, 0.44);
	}
	
</style>
